package tests;

import java.io.File;

import mastermind.DataOperator;
import mastermind.game.Player;
import mastermind.game.Score;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class ScoreTests {
	private String userLogPath = "u";
	private String scoreLogPath = "s";
	private DataOperator dao;
	int batmanScores[], jokerScores[];
	double exBatmanAv, exJokerAv, exOverallAv;
	
	@Before
	public void setUp() {
		delFile(scoreLogPath);
		delFile(userLogPath);
		dao = new DataOperator(userLogPath, scoreLogPath);
		
		Player p = new Player("batman", "batman", "batman", "batman", "1800batman");
		dao.addPlayer(p);
		p = new Player("joker", "joker", "joker", "joker", "1800joker");
		dao.addPlayer(p);
		
		exOverallAv = exBatmanAv = exJokerAv = 0;
		
		batmanScores = new int[] {10, 5, 5};
		for(int s : batmanScores) {
			dao.addScore("batman", new Score(s));
			exBatmanAv += s;
			exOverallAv += s;
		}
		exBatmanAv /= batmanScores.length;
		
		jokerScores = new int[] {-5, 3, 4};
		for(int s : jokerScores) {
			dao.addScore("joker", new Score(s));
			exJokerAv += s;
			exOverallAv += s;
		}
		exJokerAv /= jokerScores.length;
		
		exOverallAv /= (batmanScores.length + jokerScores.length);
	}

	private void delFile(String path) {
		File f = new File(path);
		if (f.exists()) {
			f.delete();
		}
	}

	@Test
	public void scoreCount() {
		Assert.assertTrue(dao.getScores().keySet().size() == 2);
		Assert.assertTrue(dao.getScores().get("batman").size() == batmanScores.length);
		Assert.assertTrue(dao.getScores().get("joker").size() == jokerScores.length);
	}

	@Test
	public void playerAverageTest() {
		double batmanAv = dao.getPlayerAverage("batman");
		Assert.assertTrue(exBatmanAv == batmanAv);
		
		double jokerAv = dao.getPlayerAverage("joker");
		Assert.assertTrue(exJokerAv == jokerAv);
	}
	
	@Test
	public void overallAverageTest() {
		double overallAv = dao.getAverageScore();
		Assert.assertTrue(exOverallAv == overallAv);
	}
}
